<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class BargainAtvSpe extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_bargain_atv_spe';


    protected $append = [

        'true_stock'

    ];

    /**
     * @author chenniang
     * @DataTime: 2021-01-20 11:46
     * @功能说明:最小库存
     */
    public function getTrueStockAttr($value,$data){

        if(!empty($data['spe_id'])&&isset($data['stock'])&&!empty($data['atv_id'])){

            $record_model = new BargainRecord();

            $dis = [

                'atv_id' => $data['atv_id'],

                'status' => 3
            ];
            //获取已经卖出的销量
            $count = $record_model->where($dis)->sum('num');

            $data['stock'] -= $count;

            $data['stock']  = $data['stock']>0?$data['stock']:0;

            $spe_model = new  IndexShopSpePrice();

            $spe_stock = $spe_model->where(['id'=>$data['spe_id']])->value('stock');

            $stock = $data['stock']<$spe_stock?$data['stock']:$spe_stock;

            return $stock;

        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:获取低价
     * @author chenniang
     * @DataTime: 2021-01-20 14:02
     */
    public function getLowPriceAttr($value,$data){

        if(!empty($data['spe_id'])&&isset($data['low_price'])){

            $spe_model = new IndexShopSpePrice();

            $spe_price = $spe_model->where(['id'=>$data['spe_id']])->value('price');

            $price = $data['low_price']<$spe_price?$data['low_price']:$spe_price;

            return $price;

        }
    }

    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:17
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-09 14:18
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-01-19 10:46
     * @功能说明:详情
     */
   public function dataInfo($dis){

       $data = $this->where($dis)->find();

       return !empty($data)?$data->toArray():[];


   }

    /**
     * @author chenniang
     * @DataTime: 2021-01-20 14:11
     * @功能说明:
     */
   public function dataSelect($atv_id,$goods_id){

       $dis[] = ['a.atv_id','=',$atv_id];

       $dis[] = ['b.goods_id','=',$goods_id];

       $dis[] = ['b.status','=',1];

       $data = $this->alias('a')
              ->join('longbing_card_shop_spe_price b','a.spe_id = b.id')
              ->where($dis)
              ->field('a.stock,a.low_price,a.spe_id,a.atv_id,a.goods_id')
              ->group('a.id')
              ->order('a.id')
              ->select()
              ->toArray();

       if(!empty($data)){

           $spe_model = new IndexShopSpePrice();

           foreach ($data as &$v){

               $spe = $spe_model->SpePriceFind(['id'=>$v['spe_id'],'status'=>1]);

               $v['spe_name'] = !empty($spe)?$spe['spe_name_text']:'';
           }
       }

       return $data;
   }




}